//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.impl.pmpub.project;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import nc.bs.bd.baseservice.MultiManageTypeBaseService;
import nc.bs.bd.cache.CacheProxy;
import nc.bs.bd.pub.distribution.util.BDDistCommonUtil;
import nc.bs.bd.service.ValueObjWithErrLog;
import nc.bs.businessevent.EventDispatcher;
import nc.bs.businessevent.UsePermChangeEvent;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.pmpub.uap.project.bp.ProjectInsertBP;
import nc.bs.pmpub.uap.project.bp.ProjectUpdateBP;
import nc.bs.uif2.validation.ValidationFailure;
import nc.bs.uif2.validation.Validator;
import nc.impl.pub.util.db.InSqlManager;
import nc.impl.pubapp.pattern.data.bill.tool.BillTransferTool;
import nc.impl.pubapp.pattern.data.vo.VOQuery;
import nc.itf.pmpub.project.pub.IProjectServiceForPu;
import nc.itf.pmpub.prv.IProject;
import nc.itf.pmpub.prv.IProjectQuery;
import nc.itf.pmpub.pub.IWBSService;
import nc.itf.uap.distribution.restrans.ITransResourceQuery;
import nc.itf.uap.distribution.restrans.ITransResourceType;
import nc.jdbc.framework.processor.ResultSetProcessor;
import nc.md.data.access.NCObject;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.pub.billcode.itf.IBillcodeManage;
import nc.pub.billcode.vo.BillCodeContext;
import nc.pubitf.org.IOrgUnitPubService;
import nc.pubitf.org.cache.IItemOrgPubService_C;
import nc.pubitf.org.cache.IOrgUnitPubService_C;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.org.ItemOrgVO;
import nc.vo.pca.costaccount.PcaProjectQryConditionVO;
import nc.vo.pmpub.common.utils.ArrayUtils;
import nc.vo.pmpub.common.utils.ListUtil;
import nc.vo.pmpub.common.utils.PMProxy;
import nc.vo.pmpub.common.utils.StringUtil;
import nc.vo.pmpub.project.ProjectBillVO;
import nc.vo.pmpub.project.ProjectBodyVO;
import nc.vo.pmpub.project.ProjectHeadVO;
import nc.vo.pmpub.wbs.WbsVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pubapp.AppContext;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.uif2.LoginContext;
import nc.vo.util.BDReferenceChecker;
import nc.vo.util.BDUniqueRuleValidate;
import nccloud.commons.lang.StringUtils;

public class ProjectImpl extends MultiManageTypeBaseService<ProjectHeadVO> implements IProject, IProjectServiceForPu {
    private IBillcodeManage billcodeManage = null;
    private IWBSService WBSService = null;
    private ITransResourceQuery resQuery = null;
    private ProjectBillVO[] deleteObs;

    public ProjectImpl() {
        super("2ee58f9b-781b-469f-b1d8-1816842515c3", ProjectHeadVO.class, (String[])null);
    }

    public void deleteProjects(ProjectHeadVO[] vos) throws BusinessException {
        if (vos[0].getUpload_flag() != null && vos[0].getUpload_flag() != UFBoolean.FALSE) {
            throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("pmbase_0", "01014p00-0130"));
        } else {
            this.deleteVO(vos);
            this.disposeBillCode4Delete(vos[0]);
            this.getWBSServce().DeleteProjectWBSByID(this.getProejctPks(vos));
            EventDispatcher.fireEvent(new UsePermChangeEvent(this.getMDId(), "1006"));
            CacheProxy.fireDataDeleted("bd_project", vos[0].getPk_project());
        }
    }

    protected Validator[] getDeleteValidator() {
        String[] exclude_tables = new String[]{WbsVO.getDefaultTableName()};
        Validator[] validators = new Validator[]{BDReferenceChecker.getInstance(exclude_tables)};
        return validators;
    }

    public ProjectHeadVO[] disEnableProjects(ProjectHeadVO[] vos) throws BusinessException {
        ValueObjWithErrLog returnValue = this.disableVO(vos);
        return (ProjectHeadVO[])((ProjectHeadVO[])returnValue.getVos());
    }

    public ProjectHeadVO[] enableProjects(ProjectHeadVO[] vos) throws BusinessException {
        ValueObjWithErrLog returnValue = this.enableVO(vos);
        return (ProjectHeadVO[])((ProjectHeadVO[])returnValue.getVos());
    }

    public ProjectHeadVO insertProject(ProjectHeadVO vo) throws BusinessException {
        boolean isDistSystem = BDDistCommonUtil.getInstance().isEnableDistSystem();
        boolean isAdd = true;
        if (isDistSystem) {
            ITransResourceType restype = null;
            if (vo.getPk_org().equals("GLOBLE00000000000000")) {
                restype = this.getTransResourceQuery().getTransResourceType("projectData_down");
            } else {
                restype = this.getTransResourceQuery().getTransResourceType("projectData_up");
            }

            isAdd = restype.getProperties().getAddEnable();
        }

        if (isAdd) {
            this.fillDefault(vo);
            this.disposeBillCode4Insert(vo);
            ProjectHeadVO[] dbVOS = (ProjectHeadVO[])this.insertVO(new ProjectHeadVO[]{vo});
            EventDispatcher.fireEvent(new UsePermChangeEvent(this.getMDId(), "1002"));
            CacheProxy.fireDataInserted("bd_project");
            return dbVOS[0];
        } else {
            throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("pmbase_0", "01014p00-0131"));
        }
    }

    private void fillDefault(ProjectHeadVO vo) {
        vo.setPk_group(InvocationInfoProxy.getInstance().getGroupId());
        vo.setBegin_flag(UFBoolean.valueOf(false));
        vo.setEnablestate(2);
        if (!"GLOBLE00000000000000".equals(vo.getPk_org()) && !vo.getPk_org().equals(vo.getPk_group())) {
            String pk_org = vo.getPk_org();
            String pk_org_v = vo.getPk_org_v();
            if (StringUtil.isEmpty(pk_org_v)) {
                try {
                    UFDate busiDate = AppContext.getInstance().getBusiDate();
                    Map<String, String> map = ((IOrgUnitPubService)NCLocator.getInstance().lookup(IOrgUnitPubService.class)).getNewVIDSByOrgIDSAndDate(new String[]{pk_org}, busiDate);
                    pk_org_v = (String)map.get(pk_org);
                    vo.setPk_org_v(pk_org_v);
                } catch (BusinessException var6) {
                    ExceptionUtils.wrappException(var6);
                }
            }

            if (StringUtil.isEmpty(vo.getPk_duty_org())) {
                vo.setPk_duty_org(vo.getPk_org());
            }

            if (StringUtil.isEmpty(vo.getPk_duty_org_v())) {
                vo.setPk_duty_org_v(vo.getPk_org_v());
            }
        }

    }

    public Map<String, Boolean> queryDisableProject(String[] pk_projectclasss) throws BusinessException {
        StringBuilder whereSql = new StringBuilder();
        Map<String, Boolean> map = new HashMap();
        whereSql.append(" enablestate = ").append(2);
        whereSql.append(" and pk_projectclass in ");
        whereSql.append(InSqlManager.getInSQLValue(pk_projectclasss));
        ProjectHeadVO[] ProjectHeadVOs = (new ProjectcQueryImpl()).queryProjectHeadVOsByCondition(whereSql.toString());
        int var6;
        int var7;
        if (ArrayUtils.isNotEmpty(ProjectHeadVOs)) {
            ProjectHeadVO[] var5 = ProjectHeadVOs;
            var6 = ProjectHeadVOs.length;

            for(var7 = 0; var7 < var6; ++var7) {
                ProjectHeadVO projectHeadVO = var5[var7];
                if (map.get(projectHeadVO.getPk_projectclass()) == null) {
                    map.put(projectHeadVO.getPk_projectclass(), false);
                }
            }
        }

        String[] var9 = pk_projectclasss;
        var6 = pk_projectclasss.length;

        for(var7 = 0; var7 < var6; ++var7) {
            String pk_projectclass = var9[var7];
            if (map.get(pk_projectclass) == null) {
                map.put(pk_projectclass, true);
            }
        }

        return map;
    }

    public Map<String, UFBoolean> queryEnableProject(String[] pk_projects) throws BusinessException {
        StringBuilder whereSql = new StringBuilder();
        Map<String, UFBoolean> map = new HashMap();
        whereSql.append(" enablestate = ").append(2);
        whereSql.append(" and pk_project in ");
        whereSql.append(InSqlManager.getInSQLValue(pk_projects));
        ProjectHeadVO[] ProjectHeadVOs = (new ProjectcQueryImpl()).queryProjectHeadVOsByCondition(whereSql.toString());
        int var6;
        int var7;
        if (ArrayUtils.isNotEmpty(ProjectHeadVOs)) {
            ProjectHeadVO[] var5 = ProjectHeadVOs;
            var6 = ProjectHeadVOs.length;

            for(var7 = 0; var7 < var6; ++var7) {
                ProjectHeadVO projectHeadVO = var5[var7];
                map.put(projectHeadVO.getPrimaryKey(), UFBoolean.TRUE);
            }
        }

        String[] var9 = pk_projects;
        var6 = pk_projects.length;

        for(var7 = 0; var7 < var6; ++var7) {
            String pk_project = var9[var7];
            if (map.get(pk_project) == null) {
                map.put(pk_project, UFBoolean.FALSE);
            }
        }

        return map;
    }

    public Map<String, List<String>> queryProjectByOrgs(String[] pk_orgs) throws BusinessException {
        Map<String, List<String>> map = new HashMap();
        StringBuilder whereSql = new StringBuilder();
        whereSql.append(" enablestate = ").append(2);
        whereSql.append(" pk_duty_org in ");
        whereSql.append(InSqlManager.getInSQLValue(pk_orgs));
        ProjectHeadVO[] ProjectHeadVOs = (new ProjectcQueryImpl()).queryProjectHeadVOsByCondition(whereSql.toString());
        if (ArrayUtils.isNotEmpty(ProjectHeadVOs)) {
            ProjectHeadVO[] var5 = ProjectHeadVOs;
            int var6 = ProjectHeadVOs.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                ProjectHeadVO projectHeadVO = var5[var7];
                if (map.get(projectHeadVO.getPk_projectclass()) == null) {
                    List<String> list = new ArrayList();
                    list.add(projectHeadVO.getPrimaryKey());
                    map.put(projectHeadVO.getPk_duty_org(), list);
                } else {
                    ((List)map.get(projectHeadVO.getPk_duty_org())).add(projectHeadVO.getPrimaryKey());
                }
            }
        }

        return map;
    }

    public Map<String, List<String>> queryProjectByType(String[] pk_projectclasss) throws BusinessException {
        Map<String, List<String>> map = new HashMap();
        StringBuilder whereSql = new StringBuilder();
        whereSql.append(" pk_projectclass in ");
        whereSql.append(InSqlManager.getInSQLValue(pk_projectclasss));
        ProjectHeadVO[] ProjectHeadVOs = (new ProjectcQueryImpl()).queryProjectHeadVOsByCondition(whereSql.toString());
        if (ArrayUtils.isNotEmpty(ProjectHeadVOs)) {
            ProjectHeadVO[] var5 = ProjectHeadVOs;
            int var6 = ProjectHeadVOs.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                ProjectHeadVO projectHeadVO = var5[var7];
                if (map.get(projectHeadVO.getPk_projectclass()) == null) {
                    List<String> list = new ArrayList();
                    list.add(projectHeadVO.getPrimaryKey());
                    map.put(projectHeadVO.getPk_projectclass(), list);
                } else {
                    ((List)map.get(projectHeadVO.getPk_projectclass())).add(projectHeadVO.getPrimaryKey());
                }
            }
        }

        return map;
    }

    public ProjectHeadVO updateProject(ProjectHeadVO vo) throws BusinessException {
        boolean isDistSystem = BDDistCommonUtil.getInstance().isEnableDistSystem();
        CacheProxy.fireDataUpdated("bd_project", vo.getPk_project());
        boolean isEdit = true;
        if (isDistSystem) {
            ITransResourceType restype = null;
            if (vo.getPk_org().equals("GLOBLE00000000000000")) {
                restype = this.getTransResourceQuery().getTransResourceType("projectData_down");
            } else {
                restype = this.getTransResourceQuery().getTransResourceType("projectData_up");
            }

            isEdit = restype.getProperties().getUpdateEnable();
        }

        if (!isEdit && vo.getUpload_flag() != UFBoolean.FALSE) {
            throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("pmbase_0", "01014p00-0129"));
        } else {
            ProjectHeadVO[] dbVOS = (ProjectHeadVO[])this.updateVO(new ProjectHeadVO[]{vo});
            EventDispatcher.fireEvent(new UsePermChangeEvent(this.getMDId(), "1004"));
            return dbVOS[0];
        }
    }

    protected void updateValidateVOs(ProjectHeadVO[] oldVOs, ProjectHeadVO... vos) throws BusinessException {
        ProjectHeadVO vo = vos[0];
        ProjectHeadVO oldVO = oldVOs[0];
        boolean changeProjectType = false;
        if (oldVO.getPk_projectclass() != null && vo.getPk_projectclass() != null && !oldVO.getPk_projectclass().equals(vo.getPk_projectclass())) {
            changeProjectType = true;
        }

        if (changeProjectType) {
            WbsVO[] wbsVOS = ((IWBSService)PMProxy.lookup(IWBSService.class)).queryCBSinWbs(vo.getPk_project());
            if (ArrayUtils.isNotEmpty(wbsVOS)) {
                throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("pmbase_0", "01014p00-0136"));
            }
        }

        super.updateValidateVOs(oldVOs, vos);
    }

    private void disposeBillCode4Delete(ProjectHeadVO vo) throws BusinessException {
        BillCodeContext billCodeContext = this.getBillcodeManage().getBillCodeContext("project", vo.getPk_group(), vo.getPk_org());
        if (billCodeContext != null) {
            this.getBillcodeManage().returnBillCodeOnDelete("project", vo.getPk_group(), vo.getPk_org(), vo.getProject_code(), vo);
        }

    }

    private void disposeBillCode4Insert(ProjectHeadVO vo) throws BusinessException {
        BillCodeContext billCodeContext = this.getBillcodeManage().getBillCodeContext("project", vo.getPk_group(), vo.getPk_org());
        if (null != billCodeContext) {
            if (billCodeContext.isPrecode()) {
                this.getBillcodeManage().commitPreBillCode("project", vo.getPk_group(), vo.getPk_org(), vo.getProject_code());
            } else {
                String code = ((IBillcodeManage)NCLocator.getInstance().lookup(IBillcodeManage.class)).getBillCode_RequiresNew("project", vo.getPk_group(), vo.getPk_org(), vo);
                vo.setProject_code(code);
            }
        } else if (StringUtils.isEmpty(vo.getProject_code())) {
            throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("pmbase_0", "01014p00-0068"));
        }

    }

    private IBillcodeManage getBillcodeManage() {
        if (this.billcodeManage == null) {
            this.billcodeManage = (IBillcodeManage)NCLocator.getInstance().lookup(IBillcodeManage.class);
        }

        return this.billcodeManage;
    }

    private String[] getProejctPks(ProjectHeadVO[] vos) {
        String[] pks = null;
        if (ArrayUtils.isNotEmpty(vos)) {
            pks = new String[vos.length];

            for(int i = 0; i < vos.length; ++i) {
                pks[i] = vos[i].getPk_project();
            }
        }

        return pks;
    }

    private ITransResourceQuery getTransResourceQuery() {
        if (this.resQuery == null) {
            this.resQuery = (ITransResourceQuery)NCLocator.getInstance().lookup(ITransResourceQuery.class);
        }

        return this.resQuery;
    }

    private IWBSService getWBSServce() {
        if (this.WBSService == null) {
            this.WBSService = (IWBSService)NCLocator.getInstance().lookup(IWBSService.class);
        }

        return this.WBSService;
    }

    public List<String> queryProjectByOrg(String pk_org) throws BusinessException {
        return this.queryProjectByOrgAndPks(pk_org, (String[])null);
    }

    public List<String> queryProjectByOrgAndPks(String pk_org, String[] pk_projects) throws BusinessException {
        StringBuilder whereSql = new StringBuilder();
        ItemOrgVO[] itemOrgVOs = this.getFinanceOrgIDsByProjOrgIDs(new String[]{pk_org});
        if (!ArrayUtils.isEmpty(itemOrgVOs)) {
            if (whereSql.length() > 0) {
                whereSql.append(" and ");
            }

            StringBuilder sql = new StringBuilder();

            for(int i = 0; i < itemOrgVOs.length; ++i) {
                if (sql.length() == 0) {
                    sql.append(" '" + itemOrgVOs[i].getPk_itemorg() + "'");
                } else {
                    sql.append(",'" + itemOrgVOs[i].getPk_itemorg() + "'");
                }
            }

            if (sql.length() > 0) {
                whereSql.append("  ( pk_org='").append("GLOBLE00000000000000").append("'");
                whereSql.append(" or (pk_org='").append(itemOrgVOs[0].getPk_group()).append("' and  pk_duty_org in (" + sql + "))");
                whereSql.append(" or pk_org in (" + sql + ")");
                whereSql.append(")");
            }

            if (!ArrayUtils.isEmpty(pk_projects)) {
                whereSql.append(" and pk_project in ").append(InSqlManager.getInSQLValue(pk_projects)).append(" ");
            }
        }

        String[] projectPks = (new ProjectcQueryImpl()).queryProjectPKSByCondition((LoginContext)null, whereSql.toString());
        List<String> pkList = new ArrayList();
        if (ArrayUtils.isNotEmpty(projectPks)) {
            String[] var7 = projectPks;
            int var8 = projectPks.length;

            for(int var9 = 0; var9 < var8; ++var9) {
                String propk = var7[var9];
                pkList.add(propk);
            }
        }

        return pkList;
    }

    public ItemOrgVO[] getFinanceOrgIDsByProjOrgIDs(String[] pk_projOrgs) {
        if (ArrayUtils.isEmpty(pk_projOrgs)) {
            return null;
        } else {
            try {
                ItemOrgVO[] projOrg2FinanceOrg = ((IItemOrgPubService_C)NCLocator.getInstance().lookup(IItemOrgPubService_C.class)).queryItemOrgIDsByFinanceOrgIDs(pk_projOrgs);
                return projOrg2FinanceOrg;
            } catch (BusinessException var3) {
                ExceptionUtils.wrappException(var3);
                return null;
            }
        }
    }

    protected ProjectHeadVO[] retrieveVOs(String[] pks) throws BusinessException {
        IMDPersistenceQueryService queryService = (IMDPersistenceQueryService)NCLocator.getInstance().lookup(IMDPersistenceQueryService.class);
        Collection<ProjectHeadVO> c = queryService.queryBillOfVOByPKs(ProjectHeadVO.class, pks, false);
        return c != null && c.size() >= 1 ? (ProjectHeadVO[])c.toArray(new ProjectHeadVO[0]) : null;
    }

    private void checkParentPro(String primaryKey, String parentPk) {
        List<String> parentList = new ArrayList();
        this.queryAllParentPk(parentPk, parentList, 0);
        if (StringUtils.isNotEmpty(primaryKey) && parentList.contains(primaryKey)) {
            ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("pmbase_0", "01014p00-0161"));
        }

    }

    private void queryAllParentPk(String parentPk, List<String> parentList, int mark) {
        if (parentPk != null) {
            VOQuery<ProjectHeadVO> query = new VOQuery(ProjectHeadVO.class);
            ProjectHeadVO[] vos = (ProjectHeadVO[])query.query(new String[]{parentPk});
            if (ArrayUtils.isEmpty(vos)) {
                ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("pmbase_0", "01014p00-0162"));
            }

            ProjectHeadVO vo = vos[0];
            parentList.add(vo.getPrimaryKey());
            parentPk = vo.getPk_parentpro();
            if (StringUtils.isEmpty(parentPk)) {
                return;
            }

            ++mark;
            if (mark == 1000) {
                ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("pmbase_0", "01014p00-0163"));
            }

            this.queryAllParentPk(parentPk, parentList, mark);
        }

    }

    public ProjectBillVO[] insert(ProjectBillVO[] ob) throws BusinessException {
        ProjectHeadVO vo = ob[0].getParentVO();
        boolean isDistSystem = BDDistCommonUtil.getInstance().isEnableDistSystem();
        boolean isAdd = true;
        if (isDistSystem) {
            ITransResourceType restype = null;
            if (vo.getPk_org().equals("GLOBLE00000000000000")) {
                restype = this.getTransResourceQuery().getTransResourceType("projectData_down");
            } else {
                restype = this.getTransResourceQuery().getTransResourceType("projectData_up");
            }

            isAdd = restype.getProperties().getAddEnable();
        }

        if (isAdd) {
            this.fillDefault(vo);
            this.disposeBillCode4Insert(vo);
            this.setBodyOrg(ob[0], vo);
            String pk_group = vo.getPk_group();
            String pk_org = vo.getPk_org();
            ValidationFailure failure = (new BDUniqueRuleValidate()).validate(vo);
            if (null != failure) {
                if ("32001".equals(failure.getErrorcode())) {
                    String code = vo.getProject_code();
                    this.getBillcodeManage().AbandonBillCode_RequiresNew("project", pk_group, pk_org, code);
                }

                throw new BusinessException(failure.getMessage());
            } else {
                ProjectBillVO[] dbVOS = (new ProjectInsertBP()).insert(ob);
//                EventDispatcher.fireEvent(new UsePermChangeEvent(this.getMDId(), "1002"));
//                CacheProxy.fireDataInserted("bd_project");
                UsePermChangeEvent usePermChangeEvent = new UsePermChangeEvent(this.getMDId(), "1002");
                usePermChangeEvent.setUserObject(dbVOS);
                EventDispatcher.fireEvent(usePermChangeEvent);
                CacheProxy.fireDataInserted("bd_project");
                return dbVOS;
            }
        } else {
            throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("pmbase_0", "01014p00-0131"));
        }
    }

    private void setBodyOrg(ProjectBillVO billVO, ProjectHeadVO headVO) {
        CircularlyAccessibleValueObject[] bodyVOs = billVO.getAllChildrenVO();
        if (null != bodyVOs && bodyVOs.length != 0) {
            String pk_group = headVO.getPk_group();
            String pk_org = headVO.getPk_org();
            String pk_org_v = headVO.getPk_org_v();
            CircularlyAccessibleValueObject[] var7 = bodyVOs;
            int var8 = bodyVOs.length;

            for(int var9 = 0; var9 < var8; ++var9) {
                CircularlyAccessibleValueObject bodyVO = var7[var9];
                if (bodyVO.getStatus() == 2) {
                    bodyVO.setAttributeValue("pk_group", pk_group);
                    bodyVO.setAttributeValue("pk_org", pk_org);
                    bodyVO.setAttributeValue("pk_org_v", pk_org_v);
                }
            }

        }
    }

    public ProjectBillVO[] update(ProjectBillVO[] ob) throws BusinessException {
        ProjectHeadVO vo = ob[0].getParentVO();
        boolean isDistSystem = BDDistCommonUtil.getInstance().isEnableDistSystem();
        CacheProxy.fireDataUpdated("bd_project", vo.getPk_project());
        boolean isEdit = true;
        if (isDistSystem) {
            ITransResourceType restype = null;
            if (vo.getPk_org() != null && vo.getPk_org().equals("GLOBLE00000000000000")) {
                restype = this.getTransResourceQuery().getTransResourceType("projectData_down");
            } else {
                restype = this.getTransResourceQuery().getTransResourceType("projectData_up");
            }

            isEdit = restype.getProperties().getUpdateEnable();
        }

        if (!isEdit && vo.getUpload_flag() != UFBoolean.FALSE) {
            throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("pmbase_0", "01014p00-0129"));
        } else {
            if (StringUtils.isNotEmpty(vo.getPk_parentpro())) {
                this.checkParentPro(vo.getPk_project(), vo.getPk_parentpro());
            }

            ProjectBillVO[] dbVOS = this.updateProjectbills(ob);
            UsePermChangeEvent usePermChangeEvent = new UsePermChangeEvent(this.getMDId(), "1004");
            usePermChangeEvent.setUserObject(vo);
            EventDispatcher.fireEvent(usePermChangeEvent);
            return dbVOS;
        }
    }

    public ProjectBillVO[] updateBill4Pfxx(ProjectBillVO[] billVos) throws BusinessException {
        try {
            ProjectBillVO[] originBills = ((IProjectQuery)NCLocator.getInstance().lookup(IProjectQuery.class)).queryProjectBillVOsByCondition("project_code ='" + billVos[0].getParentVO().getProject_code() + "'");
            this.setBodyOrg(billVos[0], billVos[0].getParentVO());
            ProjectBillVO[] retBills = (new ProjectUpdateBP()).update(billVos, originBills);
            return retBills;
        } catch (Exception var4) {
            ExceptionUtils.marsh(var4);
            return null;
        }
    }

    private ProjectBillVO[] updateProjectbills(ProjectBillVO[] billVos) throws BusinessException {
        try {
            BillTransferTool<ProjectBillVO> transTool = new BillTransferTool(billVos);
            ProjectBillVO[] fullBills = (ProjectBillVO[])transTool.getClientFullInfoBill();
            ProjectBillVO[] originBills = (ProjectBillVO[])transTool.getOriginBills();
            this.setBodyOrg(fullBills[0], fullBills[0].getParentVO());
            ProjectBillVO[] retBills = (new ProjectUpdateBP()).update(fullBills, originBills);
            return retBills;
        } catch (Exception var6) {
            ExceptionUtils.marsh(var6);
            return null;
        }
    }

    public void delete(ProjectBillVO[] ob) throws BusinessException {
        List<ProjectHeadVO> allHeadList = new ArrayList();
        ProjectBillVO[] var3 = ob;
        int var4 = ob.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            ProjectBillVO billvo = var3[var5];
            allHeadList.add(billvo.getParentVO());
        }

        ProjectHeadVO[] vos = (ProjectHeadVO[])ListUtil.toArray(allHeadList);
        if (vos[0].getUpload_flag() != null && vos[0].getUpload_flag() != UFBoolean.FALSE) {
            throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("pmbase_0", "01014p00-0130"));
        } else {
            this.deleteObs = ob;
            this.deleteVO(vos);
            this.disposeBillCode4Delete(vos[0]);
            this.getWBSServce().DeleteProjectWBSByID(this.getProejctPks(vos));
            EventDispatcher.fireEvent(new UsePermChangeEvent(this.getMDId(), "1006"));
            CacheProxy.fireDataDeleted("bd_project", vos[0].getPk_project());
        }
    }

    protected void dbDeleteVOs(ProjectHeadVO... vos) throws BusinessException {
        List<NCObject> ncObjectArray = new ArrayList();
        ProjectBillVO[] var3 = this.deleteObs;
        int var4 = var3.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            ProjectBillVO billVo = var3[var5];
            NCObject ncObj = NCObject.newInstance(billVo);
            ncObjectArray.add(ncObj);
        }

        getMDService().deleteBillFromDB((NCObject[])ListUtil.toArray(ncObjectArray));
    }

    public ValueObjWithErrLog disableFun(ProjectHeadVO[] vos) throws BusinessException {
        return this.disableVO(vos);
    }

    public ValueObjWithErrLog enableFun(ProjectHeadVO[] vos) throws BusinessException {
        return this.enableVO(vos);
    }

    public void assignOrgs(String[] pks, String[] orgs) throws BusinessException {
        if (!ArrayUtils.isEmpty(pks) && !ArrayUtils.isEmpty(orgs)) {
            HashMap<String, String> org_orgvMap = ((IOrgUnitPubService_C)PMProxy.lookup(IOrgUnitPubService_C.class)).getNewVIDSByOrgIDS(orgs);
            ProjectBillVO[] billvos = ((IProjectQuery)PMProxy.lookup(IProjectQuery.class)).queryProjectBillVOsBypks(pks);
            ProjectBillVO[] var5 = billvos;
            int var6 = billvos.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                ProjectBillVO billvo = var5[var7];
                ProjectHeadVO headvo = billvo.getParentVO();
                ProjectBodyVO[] bodyvos = billvo.getProjectBodyVO();
                Set<String> bodyorgSet = new HashSet();
                List<ProjectBodyVO> bodyList = new ArrayList();
                int var14;
                int var15;
                if (ArrayUtils.isNotEmpty(bodyvos)) {
                    ProjectBodyVO[] var13 = bodyvos;
                    var14 = bodyvos.length;

                    for(var15 = 0; var15 < var14; ++var15) {
                        ProjectBodyVO bodyvo = var13[var15];
                        String pk_parti_org = bodyvo.getPk_parti_org();
                        bodyorgSet.add(pk_parti_org);
                        bodyList.add(bodyvo);
                    }
                }

                String[] var18 = orgs;
                var14 = orgs.length;

                for(var15 = 0; var15 < var14; ++var15) {
                    String org = var18[var15];
                    if (!bodyorgSet.contains(org)) {
                        ProjectBodyVO newbodyvo = new ProjectBodyVO();
                        newbodyvo.setPk_group(headvo.getPk_group());
                        newbodyvo.setPk_org(headvo.getPk_org());
                        newbodyvo.setPk_org_v(headvo.getPk_org_v());
                        newbodyvo.setPk_parti_org(org);
                        newbodyvo.setPk_parti_org_v((String)org_orgvMap.get(org));
                        newbodyvo.setStatus(2);
                        newbodyvo.setAttributeValue("pk_project", headvo.getPk_project());
                        bodyList.add(newbodyvo);
                    }
                }

                billvo.setChildrenVO((CircularlyAccessibleValueObject[])ListUtil.toArray(bodyList));
                headvo.setStatus(0);
            }

            this.updateProjectbills(billvos);
        }
    }

    public void cancelAssignOrgs(String[] pks, String[] orgs) throws BusinessException {
        if (!ArrayUtils.isEmpty(pks) && !ArrayUtils.isEmpty(orgs)) {
            Set<String> cancelorgSet = new HashSet();
            String[] var4 = orgs;
            int var5 = orgs.length;

            int var6;
            for(var6 = 0; var6 < var5; ++var6) {
                String cancelOrg = var4[var6];
                cancelorgSet.add(cancelOrg);
            }

            ProjectBillVO[] billvos = ((IProjectQuery)PMProxy.lookup(IProjectQuery.class)).queryProjectBillVOsBypks(pks);
            ProjectBillVO[] var18 = billvos;
            var6 = billvos.length;

            for(int var19 = 0; var19 < var6; ++var19) {
                ProjectBillVO billvo = var18[var19];
                ProjectHeadVO headvo = billvo.getParentVO();
                ProjectBodyVO[] bodyvos = billvo.getProjectBodyVO();
                List<ProjectBodyVO> bodyList = new ArrayList();
                if (ArrayUtils.isNotEmpty(bodyvos)) {
                    ProjectBodyVO[] var12 = bodyvos;
                    int var13 = bodyvos.length;

                    for(int var14 = 0; var14 < var13; ++var14) {
                        ProjectBodyVO bodyvo = var12[var14];
                        String pk_parti_org = bodyvo.getPk_parti_org();
                        if (cancelorgSet.contains(pk_parti_org)) {
                            bodyvo.setStatus(3);
                        }

                        bodyList.add(bodyvo);
                    }
                }

                billvo.setChildrenVO((CircularlyAccessibleValueObject[])ListUtil.toArray(bodyList));
                headvo.setStatus(0);
            }

            this.updateProjectbills(billvos);
        }
    }

    public String[] queryProjectByVO(PcaProjectQryConditionVO pcaCondition) throws BusinessException {
        if (pcaCondition != null && (pcaCondition == null || !StringUtils.isEmpty(pcaCondition.getPk_org()))) {
            List<String> resultList = new ArrayList();
            String pk_org = pcaCondition.getPk_org();
            String[] pk_epss = pcaCondition.getPk_epss();
            String[] pk_projects = pcaCondition.getPk_projects();
            String[] pk_projectTypes = pcaCondition.getPk_projectypes();
            String[] projectFlags = pcaCondition.getProjectflags();
            String[] pk_projectStates = pcaCondition.getProjectstates();
            boolean isContainProject_Flag = !ArrayUtils.isEmpty(projectFlags);
            StringBuilder whereSql = new StringBuilder();
            ItemOrgVO[] itemOrgVOs = this.getFinanceOrgIDsByProjOrgIDs(new String[]{pk_org});
            StringBuilder ort_sql = new StringBuilder();
            if (!ArrayUtils.isEmpty(itemOrgVOs)) {
                if (whereSql.length() > 0) {
                    whereSql.append(" and ");
                }

                for(int i = 0; i < itemOrgVOs.length; ++i) {
                    if (ort_sql.length() == 0) {
                        ort_sql.append(" '" + itemOrgVOs[i].getPk_itemorg() + "'");
                    } else {
                        ort_sql.append(",'" + itemOrgVOs[i].getPk_itemorg() + "'");
                    }
                }

                if (ort_sql.length() > 0) {
                    if (!isContainProject_Flag) {
                        whereSql.append("  ( pk_org='").append("GLOBLE00000000000000").append("'");
                        whereSql.append(" or (pk_org='").append(itemOrgVOs[0].getPk_group()).append("' and  pk_duty_org in (" + ort_sql + "))");
                        whereSql.append(" or pk_org in (" + ort_sql + ")");
                        whereSql.append(")");
                    } else {
                        whereSql.append("  ( bd_project.pk_org='").append("GLOBLE00000000000000").append("'");
                        whereSql.append(" or (bd_project.pk_org='").append(itemOrgVOs[0].getPk_group()).append("' and  bd_project.pk_duty_org in (" + ort_sql + "))");
                        whereSql.append(" or bd_project.pk_org in (" + ort_sql + ")");
                        whereSql.append(")");
                    }
                }
            }

            if (!ArrayUtils.isEmpty(pk_epss)) {
                this.appendWhereSqlParam("bd_project.pk_eps", pk_epss, whereSql, false);
            }

            if (!ArrayUtils.isEmpty(pk_projects)) {
                this.appendWhereSqlParam("bd_project.pk_project", pk_projects, whereSql, false);
            }

            if (!ArrayUtils.isEmpty(pk_projectTypes)) {
                this.appendWhereSqlParam("bd_project.pk_projectclass", pk_projectTypes, whereSql, false);
            }

            if (isContainProject_Flag) {
                BaseDAO dao = new BaseDAO();
                String querySql = "select bd_project.pk_project from bd_project left join pm_finalflag  on bd_project.pk_project=pm_finalflag.pk_project where " + whereSql.toString();
                StringBuffer flagWhereSql = new StringBuffer();
                String[] var16 = projectFlags;
                int var17 = projectFlags.length;

                for(int var18 = 0; var18 < var17; ++var18) {
                    String param = var16[var18];
                    flagWhereSql.append(param + ",");
                }

                if (flagWhereSql.toString().endsWith(",")) {
                    flagWhereSql.deleteCharAt(flagWhereSql.length() - 1);
                }

                querySql = querySql + " and pm_finalflag.flag_value in (" + flagWhereSql + ")";
                resultList = (ArrayList)dao.executeQuery(querySql, new ResultSetProcessor() {
                    public Object handleResultSet(ResultSet rs) throws SQLException {
                        List<String> pk_projects = new ArrayList();

                        while(rs.next()) {
                            pk_projects.add(rs.getString("pk_project"));
                        }

                        return pk_projects;
                    }
                });
            } else {
                ProjectHeadVO[] ProjectHeadVOs = (new ProjectcQueryImpl()).queryProjectHeadVOsByCondition(whereSql.toString());
                if (!ArrayUtils.isEmpty(ProjectHeadVOs)) {
                    ProjectHeadVO[] var22 = ProjectHeadVOs;
                    int var23 = ProjectHeadVOs.length;

                    for(int var24 = 0; var24 < var23; ++var24) {
                        ProjectHeadVO projectHeadVO = var22[var24];
                        resultList.add(projectHeadVO.getPrimaryKey());
                    }
                }
            }

            return (String[])resultList.toArray(new String[resultList.size()]);
        } else {
            return null;
        }
    }

    private void appendWhereSqlParam(String field_name, String[] params, StringBuilder whereSql, boolean isInterger) {
        if (whereSql.length() > 0) {
            whereSql.append(" and ");
        }

        whereSql.append(" " + field_name + " in (");
        String[] var5 = params;
        int var6 = params.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            String param = var5[var7];
            whereSql.append(isInterger ? param + "," : "'" + param + "',");
        }

        if (whereSql.toString().endsWith(",")) {
            whereSql.deleteCharAt(whereSql.length() - 1);
        }

        whereSql.append(")");
    }
}
